<template>
	<view class="loading">
		<view class="title">欢迎登录图书商城</view>
		<u--input class="email" v-model="email" placeholder="请输入邮箱" border="surround" clearable></u--input>
		<u--input class="password" v-model="password" placeholder="请输入密码" type="password" border="surround" clearable></u--input>
		<u-button class="submit" @click="submit" text="登录" type="primary" :disabled="isSubmit"></u-button>
		<view class="other">
			<navigator>找回密码</navigator>
			<navigator url="/pages/auth/register">注册</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: '',
				password: '',
				// isSubmit: false
			}
		},
		onLoad() {
			
		},
		computed: {
			isSubmit() {
				if (uni.$u.test.email(this.email) && uni.$u.test.rangeLength(this.password, [6, 20])) {
					return false
				}
				return true
			}
		},
		methods: {
			async submit() {
				const res = await uni.$u.http.post('/api/auth/login', {
					email: this.email,
					password: this.password
				})
				// console.log(res)
				this.$u.vuex('vuex_token', res.access_token)
				// 获取用户信息
				const userInfo = await uni.$u.http.get('/api/user')
				this.$u.vuex('vuex_user', userInfo)
				// console.log(userInfo)
				uni.$u.toast('登录成功')
				// 登录之后，跳转到来源页
				const backUrl = uni.getStorageSync('backUrl')
				const options = {
					type: 'reLaunch',
					url: backUrl.route || 'pages/index/index',
					params: backUrl.options || {}
				}
				setTimeout(() => {
					uni.$u.route(options)
				}, 1000)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.loading {
		padding: 100rpx;
		.title {
			font-size: 60rpx;
			text-align: center;
			margin-bottom: 60rpx;
		}
		.password {
			margin: 40rpx 0;
		}
		.submit {
			margin-bottom: 20rpx;
			color: #FFFFFF;
		}
		.other {
			display: flex;
			justify-content: space-between;
		}
	}
</style>
